#include <cairo.h>  
#include <gtk/gtk.h>  
   
   
gchar text[7][2] = {"Z","e","t","C","o","d","e"};  
gboolean timer = TRUE;  
   
   
static gboolean  
on_expose_event(GtkWidget *widget,  
    GdkEventExpose *event,  
    gpointer data)  
{  
  static gboolean flag = TRUE;
  cairo_t *cr;  
  cairo_text_extents_t extents;  
   
  static gdouble alpha = 1.0;  
  static gdouble size = 1;  
   
   
  gint x = widget->allocation.width / 2;  
  gint y = widget->allocation.height / 2;  
   
  cr = gdk_cairo_create(widget->window);  
   
  cairo_set_source_rgb(cr, 0.5, 0, 0);  
  cairo_paint(cr);  
   
  cairo_select_font_face(cr,"Courier",  
      CAIRO_FONT_SLANT_NORMAL,  
      CAIRO_FONT_WEIGHT_BOLD);  
   
  if( flag == TRUE ){ 
      size += 0.8;  
  }else{
      size -= 0.8;  
  }


  /* 根据flag控制大小和透明度 */
  if(size > 40 && flag == TRUE) {  
      alpha += 0.1;  
      size = 0;
  }else if(size < 0 && flag == FALSE) {  

      size = 40;
      alpha -= 0.1;  
  }  

  if( alpha >= 1 ){
      flag = FALSE;
  }else if( alpha <= 0 ){
      flag = TRUE;
  }



  /* 设置大小 */ 
  cairo_set_font_size(cr, size);  
   
  cairo_set_source_rgb(cr, 1, 1, 1);  
  /* 获取文本尺寸 */
  cairo_text_extents(cr,"ZetCode", &extents);  
  /* 根据文本尺寸来将文本定位在窗口的中心位置 */
  cairo_move_to(cr, x - extents.width/2, y);  
  /* 获取文本的的路径，并将其设为当前的裁剪域 */
  cairo_text_path(cr,"ZetCode");  
  cairo_clip(cr);  
  cairo_stroke(cr);  
  cairo_paint_with_alpha(cr, alpha);  
   
   
  cairo_destroy(cr);  
   
  return FALSE;  
}  
   
static gboolean  
time_handler (GtkWidget *widget)  
{  
  if(widget->window == NULL)return FALSE;  
  
  //发送expose-event信号
  gtk_widget_queue_draw(widget);  
   
  return TRUE;  
}  
   
   
int main (int argc,char*argv[])  
{  
  GtkWidget *window;  
   
  gtk_init(&argc, &argv);  
   
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);  
   
  g_signal_connect(window,"expose-event",  
      G_CALLBACK(on_expose_event), NULL);  
  g_signal_connect(window,"destroy",  
      G_CALLBACK(gtk_main_quit), NULL);  
   
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);  
  gtk_window_set_default_size(GTK_WINDOW(window), 350, 200);  
  gtk_window_set_title(GTK_WINDOW(window),"puff");  
  gtk_widget_set_app_paintable(window, TRUE);  
  
  /* 每隔14ms调用一次time_handler */ 
  g_timeout_add(14, (GSourceFunc) time_handler, (gpointer) window);  
   
  gtk_widget_show_all(window);  
   
  gtk_main();  
   
  return 0;  
}  